package com.version2_0.service.implement;

import com.alibaba.fastjson.JSONArray;
import com.util.aliyun.OCSutils;
import com.util.format.ReevesMap;
import com.version2_0.dao.SearchDao2_0;
import com.version2_0.service.SearchService2_0;
import org.apache.commons.collections.map.HashedMap;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by reeveslee on 16/6/16.
 */
@Service
public class SearchServiceImpl2_0 implements SearchService2_0 {
    private static final Logger LOGGER = Logger.getLogger(SearchServiceImpl2_0.class);

    @Autowired
    private SearchDao2_0 searchDao20;


    @Override
    public String searchPage() {
        ReevesMap reevesMap = new ReevesMap();
        List<Map<String, Object>> hotKeys = searchDao20.hotKeys();
        List<Map<String, Object>> hotPages = searchDao20.hotPages();

        JSONArray jsonArray = new JSONArray();
        if (hotKeys != null) {
            for (int i = 0; i < hotKeys.size(); i++) {
                jsonArray.add((String) hotKeys.get(i).get("keyname"));
            }
        }

        if (hotPages != null) {
            for (Map<String, Object> item : hotPages) {
                item.replace("titleImage", JSONArray.parse((String) item.get("titleImage")));
            }
        }

        reevesMap.put("hotkeys", jsonArray);
        reevesMap.put("hotpages", hotPages);
        reevesMap.put("part", searchPart());
        return reevesMap.toJsonString();
    }

    @Override
    public String search(String key) {
        ReevesMap reevesMap = new ReevesMap();
        // List<Map<String, Object>> votes = searchDao20.votes(key);
        List<Map<String, Object>> videos = searchDao20.videos(key);
        List<Map<String, Object>> pages = searchDao20.pages(key);

        // reevesMap.put("votes", votes);
        reevesMap.put("videos", videos);
        reevesMap.put("pages", pages);
        reevesMap.put("key", key);
        return reevesMap.toJsonString();
    }

    public List<Map<String, Object>> searchPart() {

        OCSutils ocSutils = new OCSutils();

        if (ocSutils.get("searchPart") == null) {
            List<Map<String, Object>> searchPartList = new ArrayList<>();
            Map<String, Object> vote = new HashedMap();
            Map<String, Object> history = new HashedMap();
            Map<String, Object> video = new HashedMap();

            vote.put("type", 3); // 投票
            vote.put("name", "vote");
            vote.put("image", "http://kuibutech.oss-cn-shanghai.aliyuncs.com/kbApp_fixedImage/vote.png");

            history.put("type", 8); // 历史上今天
            history.put("name", "history");
            history.put("image", "http://kuibutech.oss-cn-shanghai.aliyuncs.com/kbApp_fixedImage/history.png");

            video.put("type", 4); // 视频
            video.put("name", "video");
            video.put("image", "http://kuibutech.oss-cn-shanghai.aliyuncs.com/kbApp_fixedImage/vedio.png");

            searchPartList.add(vote);
            searchPartList.add(history);
            searchPartList.add(video);
            ocSutils.save("searchPart", searchPartList, 15 * 24 * 3600);

            return searchPartList;
        } else {
            return (List<Map<String, Object>>) ocSutils.get("searchPart");
        }


    }
}
